/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.ui;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.network.MarketAPI;
import com.mol.market.common.util.ScreenRules;
import com.mol.market.common.util.Utils;
import com.mol.market.common.util.ScreenRules.Rules;
import com.mol.market.demo.widget.BaseFragmentActivity.KeyStore;
import com.mol.market.demo.widget.LazyLoadFragmentWrapper;
import com.mol.market.ui.core.GridAdapter;
import com.mol.market.ui.core.GridAdapter.OnItemClickListener;
import com.mol.market.ui.core.LazyLoadAdapter;
import com.mol.market.ui_v4.ProductDetailActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author Chaos	
 * @Description TODO
 * @date 2013-1-11 
 */
public class AppFragment extends LazyLoadFragmentWrapper{
    
    @Override
    protected ArrayList<HashMap<String, Object>> getDataFromResult(int method, Object obj) {
        ArrayList<HashMap<String, Object>> data = (ArrayList<HashMap<String, Object>>) ((HashMap<String, Object>) obj)
                .get(Constants.KEY_PRODUCT_LIST);
        return data;
    }

    @Override
    protected void load() {
        MarketAPI.getRecommendProducts(getActivity(), this,
//                "778",
                "896",
                getSize(), getStart());
    }

    @Override
    protected LazyLoadAdapter initLazyLoadAdapter() {
        GridAdapter adapter = new GridAdapter(
                this, 
                null, 
                ScreenRules.getLayout(getWidthMatching(), Rules.Download),
                new String[] { 
                        Constants.KEY_PRODUCT_ICON_URL,
                        Constants.KEY_PRODUCT_NAME,
                        Constants.KEY_PRODUCT_AUTHOR,
                        Constants.KEY_PRODUCT_IS_STAR,
                        Constants.KEY_PRODUCT_DOWNLOAD_COUNT,
                        Constants.KEY_PRODUCT_DOWNLOAD,
                        Constants.KEY_PRODUCT_SIZE,
                        "flag"
                        }, 
                new int[] {
                        R.id.iv_logo,
                        R.id.tv_name,
                        R.id.tv_description,
                        R.id.iv_star,
                        R.id.tv_downloading,
                        R.id.tv_download,
                        R.id.tv_size,
                        R.id.flag
                        });
        
        adapter.setNumColumns(ScreenRules.getNumColumns(getWidthMatching(), Rules.Download));
        adapter.setProductList();
        
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int pos, int realPos, HashMap<String, Object> item) {
                String pid = (String) item.get(Constants.KEY_PRODUCT_ID);
                String name = (String) item.get(Constants.KEY_PRODUCT_NAME);
                Intent detailIntent = new Intent(getActivity(), ProductDetailActivity.class);
                detailIntent.putExtra(Constants.EXTRA_PRODUCT_ID, pid);
                detailIntent.putExtra(Constants.EXTRA_PRODUCT_NAME, name);
                detailIntent.putExtra(Constants.EXTRA_SOURCE_TYPE, Constants.SOURCE_TYPE_GFAN);
                getActivity().startActivity(detailIntent);
                Utils.trackEvent(getApplicationContext(), "应用", "应用推荐列表-进入详情页");
            }
        });
        
        return adapter;
    }
    
    @Override
    protected View initHeaderView(LayoutInflater inflater) {
        View root = inflater.inflate(R.layout.headerview_app, null);
        View app = root.findViewById(R.id.rl_app);
        View tags = root.findViewById(R.id.rl_label);
        app.setOnClickListener(this);
        tags.setOnClickListener(this);
        return root;
    }
    
    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.rl_app) {
            Utils.jumpToPage(getActivity(), SecondFragmentActivity.class, false,
                    new String[]{
                            KeyStore.KEY_ENTRY
                    },
                    new Serializable[]{
                            KeyStore.TARGET_CATEGORY_APP
                    }
            );
            Utils.trackEvent(getApplicationContext(), "应用", "应用分类");
        } else if (v.getId() == R.id.rl_label) {
            Utils.jumpToPage(getActivity(), SecondFragmentActivity.class, false,
                    new String[]{
                            KeyStore.KEY_ENTRY
                    },
                    new Serializable[]{
                            KeyStore.TARGET_CATEGORY_TAG_APP}
            );
        }
    }
    
}
